package cn.edu.xmu.oomall.order.service;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.dao.OrderItemDao;
import cn.edu.xmu.oomall.order.microservice.*;
import cn.edu.xmu.oomall.order.microservice.vo.FreightCalculatingPostVo;
import cn.edu.xmu.oomall.order.microservice.vo.FreightCalculatingRetVo;
import cn.edu.xmu.oomall.order.microservice.vo.FreightModelRetVo;
import cn.edu.xmu.oomall.order.microservice.vo.ProductRetVo;
import cn.edu.xmu.oomall.order.model.bo.Order;
import cn.edu.xmu.oomall.order.model.bo.OrderItem;
import cn.edu.xmu.oomall.order.model.po.OrderItemPo;
import cn.edu.xmu.oomall.order.model.vo.OrderItemVo;
import cn.edu.xmu.oomall.order.util.ValidList;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

@Service
public class OrderItemService {
    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FreightService freightService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private CustomerService customerService;

    /**
     * 计算一批订单明细的运费
     * goodsService.getFreightModels；getProductDetails
     * freightService.calculateFreight
     */
    public ReturnObject calculateFreight(Long regionId, List<OrderItem> orderItems){
        ValidList<FreightCalculatingPostVo> orderItemVos=new ValidList<>();
        for(OrderItem orderItem:orderItems){
            //获取运费模板Id
            Long freightId=((FreightModelRetVo)goodsService.getFreightModels(orderItem.getShopId(), orderItem.getProductId()).getData()).getId();
            Integer weight=((ProductRetVo)goodsService.getProductDetails(orderItem.getProductId()).getData()).getWeight().intValue();
            FreightCalculatingPostVo vo= cloneVo(orderItem,FreightCalculatingPostVo.class);
            vo.setFreightId(freightId);
            vo.setWeight(weight);
            vo.setQuantity(orderItem.getQuantity().intValue());
            orderItemVos.add(vo);
        }
        FreightCalculatingRetVo freightCalculatingRetVo=(FreightCalculatingRetVo)freightService.calculateFreight(regionId,orderItemVos).getData();
        return new ReturnObject(freightCalculatingRetVo);
    }

}
